//
//  LJJRegistNextViewController.m
//  shopTest
//
//  Created by 梁佳佳 on 2016/11/15.
//  Copyright © 2016年 梁佳佳. All rights reserved.
//

#import "LJJRegistNextViewController.h"
#import "LJJRegistNextView.h"

@interface LJJRegistNextViewController ()

@property (strong, nonatomic) LJJRegistNextView *nextView;    /** 注册下一步的view **/

@end

@implementation LJJRegistNextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"验证手机号";
    self.edgesForExtendedLayout = 0;
    
    LJJLog(@"用户名和密码是:%@",_userMessageDic);
    [self configUI];
    [self requestCodeNumber];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
#pragma mark -
#pragma mark - method
- (void)configUI
{
    [self.view addSubview:self.nextView];
    __weak typeof (self) weakSelf = self;
    [_nextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(weakSelf.view);
        make.height.equalTo(@140);
    }];
}
//请求验证码
- (void)requestCodeNumber{
    [self postData:@"appMember/createCode.do" param:@{@"MemberId":_userMessageDic[@"userName"]} success:^(id responseObject) {
        LJJLog(@"responseObject : %@",responseObject);
        if ([responseObject[@"result"] isEqualToString:@"success"]) {
            [self.nextView GCDTime];
        }else if ([responseObject[@"result"] isEqualToString:@"TelephoneExistError"]){
            [self showTostMessage:@"手机号已经被注册"];
        }else{
            [self showTostMessage:@"验证码请求失败"];
        }
    } error:^(NSError *error) {
        LJJLog(@"error : %@",error);
    }];
}
//注册方法
- (void)registeMethod:(NSString *)code{
    [self getData:@"appMember/appRegistration.do" param:@{@"LoginName":_userMessageDic[@"userName"],@"Lpassword":_userMessageDic[@"password"],@"Code":code,@"Telephone":_userMessageDic[@"userName"]}
          success:^(id responseObject) {
              if ([responseObject[@"result"]isEqualToString:@"success"]) {
                  [self showTostMessage:@"注册成功"];
                  [self autoLogin];
              }else if ([responseObject[@"result"]isEqualToString:@"codeError"]){
                  [self showTostMessage:@"验证码错误"];
              }else{
                  [self showTostMessage:@"注册失败"];
              }
              LJJLog(@"responseObject : %@",responseObject);
          } error:^(NSError *error) {
              LJJLog(@"error : %@",error);
          }];
}
//登录方法
- (void)autoLogin{
    [self getData:@"appMember/appLogin.do" param:@{@"LoginName":_userMessageDic[@"userName"],@"Lpassword":_userMessageDic[@"password"]} success:^(id responseObject) {
        LJJLog(@"responseObject : %@",responseObject);
        if ([responseObject[@"result"] isEqualToString:@"0"]) {
            [[NSUserDefaults standardUserDefaults] setObject:responseObject forKey:@"ISLOGIN"];
            [self performSelector:@selector(popMyViewController) withObject:nil afterDelay:1.0];
        }
    } error:^(NSError *error) {
        LJJLog(@"error : %@",error);
    }];
}

- (void)popMyViewController{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark - lazy method
- (LJJRegistNextView *)nextView{
    if (!_nextView) {
        _nextView = [[LJJRegistNextView alloc]init];
        _nextView.phoneNumString = _userMessageDic[@"userName"];
        __weak typeof (self) weakSelf = self;
        _nextView.registBlock = ^(NSString *code){
            [weakSelf registeMethod:code];
        };
    }
    return _nextView;
}



@end
